package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.EmployeeLoginService;
import com.clqteam.service.SubmitWholeSaleOrderService;
import com.clqteam.service.submitOrderService;
import com.clqteam.serviceimpl.EmployeeLoginServiceImpl;
import com.clqteam.serviceimpl.SubmitWholeSaleOrderServiceImpl;
import com.clqteam.serviceimpl.submitOrderServiceImpl;

/**
 * 提交批发订单控制层
 * @author ZHX
 *
 */
public class SubmitWholeSaleOrderController extends HttpServlet{
	
	SubmitWholeSaleOrderService swsoSer = new SubmitWholeSaleOrderServiceImpl();
	EmployeeLoginService elgSer = new EmployeeLoginServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF8");
		resp.setCharacterEncoding("UTF8");
		
		HttpSession session = req.getSession();
		Employee e = (Employee)session.getAttribute("employee");
		if(e==null){
			resp.sendRedirect("../../../login.jsp");
		}else{
		int eId = e.geteId();
		String tbName = "s_WholeSaleOrder_"+eId;
		
		int oClassId = 2;		
		String dis = req.getParameter("discount");
		double discount = 1;
		if(dis.equals("")){
		
		}else{
			discount = Double.parseDouble(dis);
		}
		
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date oDate = new Date();
		String oTime = sdf1.format(oDate);
		
		String code = oTime+eId;		
	
		String drTime = sdf2.format(oDate);	
		String drInfo = "完成批发订单"+code;
		
		double oMoney = swsoSer.submitWholeSaleOrder(code, tbName, discount, oClassId, eId, drTime);

		elgSer.insertLoginTime(drTime, drInfo, eId,e.geteName());
		
		PrintWriter pw = resp.getWriter();
		pw.write(String.valueOf(oMoney));
		pw.flush();
		pw.close();
		}
	}
}
